package com.mediaideas.videoengine.lyrics;

import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;

import com.mediaideas.videoengine.IMediaPlaybackService;

import java.util.TimerTask;

/**
 * Created by XieMing on 2016/3/26.
 */
public class LrcTask extends TimerTask {
    private LrcView mLrcView;
    private IMediaPlaybackService mMediaPlaybackService;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private long mBeginTime = -1;

    public LrcTask(LrcView lrcView, IMediaPlaybackService playbackService) {
        this.mLrcView = lrcView;
        this.mMediaPlaybackService = playbackService;
    }

    @Override
    public void run() {
        if(mBeginTime == -1) {
            mBeginTime = System.currentTimeMillis();
        }

        try {
            if (mMediaPlaybackService != null) {
                final long timePassed = mMediaPlaybackService.position();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mLrcView.seekLrcToTime(timePassed);
                    }
                });
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
